package tjcu.archives.service.impl;


import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tjcu.archives.config.UserRealm;
import tjcu.archives.entity.Menu;
import tjcu.archives.entity.MenuExample;
import tjcu.archives.entity.User;
import tjcu.archives.exception.SAMException;
import tjcu.archives.mapper.MenuMapper;
import tjcu.archives.mapper.UserMapper;
import tjcu.archives.service.MenuService;
import tjcu.archives.vo.ExampleUtil;
import tjcu.archives.vo.R;
import tjcu.archives.vo.SamEnumVO;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class MenuServiceImpl implements MenuService {

    private Logger log = LoggerFactory.getLogger(MenuServiceImpl.class);

    @Resource
    private MenuMapper menuMapper;
    
    @Resource
    private UserMapper userMapper;

    @Override
    public User loginname(String loginname) {
        //2. 封装查询条件
        MenuExample example = new MenuExample();
        MenuExample.Criteria criteria = example.createCriteria();
        //2. 设置查询条件
        criteria.andAvailableEqualTo(1);
        System.out.println(loginname);
        //4. 返回
        return menuMapper.loginname(loginname);
    }

    @Override
    public R updateMenu(Menu menu) {
        menu.setTarget("");
        menu.setAvailable(1);
        int i = menuMapper.insertSelective(menu);

        if (i == 1) {
            return R.ok("修改成功！");
        } else {
            log.error("菜单更新失败，Menu={}", menu);
            throw new SAMException(SamEnumVO.UPDATE_ERROR);
        }

    }

    @Override
    public PageInfo<Menu> findByTitle(String title, Integer page, Integer limit) {
        //1. 执行分页
        PageHelper.startPage(page, limit);

        //2. 封装查询条件
        MenuExample example = new MenuExample();
        MenuExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike(ExampleUtil.any(title));
        }

        //3. 执行查询
        List<Menu> list = menuMapper.selectByExample(example);

        //4. 封装数据
        PageInfo<Menu> pageInfo = new PageInfo<>(list);

        //5. 返回
        return pageInfo;
    }

    @Override
    public List<Menu> queryMenuByUid(Integer userid) {
        return menuMapper.queryMenuByUid(userid);
    }

    @Override
    public void register(String loginname, String password, String pwd) {
        User user = new User();
        if (password.equals(pwd)) {
            if (!StringUtils.isEmpty(loginname) && !StringUtils.isEmpty(password)) {
                String salt = UUID.randomUUID().toString();
                String pwd1 = new Md5Hash(password, salt, UserRealm.HASH_ITERATIONS).toString();
                user.setLoginName(loginname);
                user.setPassword(pwd1);
                user.setSalt(salt);
                user.setAvailable(1);

                int count = userMapper.insertSelective(user);
                //3. 健壮性判断
                if (count != 1) {
                    log.error("【注册用户信息】 添加失败！ user = {}", user);
                    throw new SAMException(SamEnumVO.USER_ADD_ERROR);
                }
            }

        }else{
            log.error("两次密码不一致");
        }
    }

    @Override
    public List<Menu> queryMenuAllList(Menu menu) {
        //2. 封装查询条件
        MenuExample example = new MenuExample();
        example.or(example.createCriteria().andTitleLike("%" + menu.getTitle() + "%"));
        List<Menu> menus = menuMapper.selectByExample(example);

        return menus;
    }

    @Override
    public int addMenu(Menu menu) {
        return menuMapper.insertSelective(menu);
    }

    @Override
    public int deleteMenu(Integer id) {
        return menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int checkMenuHasChildren(Integer pid) {
        return menuMapper.checkMenuHasChildren(pid);
    }

}

